package stations.QuakeInfo;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class MapCustomizedOverlay extends ItemizedOverlay<OverlayItem>{
	private ArrayList<OverlayItem> mapOverlays;
	private Context context;
	
	ArrayList<CirclePointslist> selectedpoints = new ArrayList<CirclePointslist>();
	
	int radius = 0;
	private Boolean markerTapped = false;
//	GeoPoint tapPosition;
	OverlayItem item;
	public MapCustomizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
		mapOverlays = new ArrayList<OverlayItem>();
         populate();
	}
		
	public MapCustomizedOverlay(Drawable defaultMarker, Context context) {
		this(defaultMarker);
		this.context = context;
	}
	@Override
	protected OverlayItem createItem(int i) {
		return mapOverlays.get(i);
	}
	@Override
	public int size() {
		return mapOverlays.size();
	}

	public boolean onTap(int index) {//GeoPoint geoPoint, MapView mapView) {
		markerTapped = true;
		item = mapOverlays.get(index);
		//Projection projection = mapView.getProjection();
       // tapPosition = projection.toPixels(geoPoint, null);
	//	tapPosition = item.getPoint();
		String sam = item.getTitle();
		double tempdist = Double.parseDouble(item.getSnippet())*0.1;
		radius = (int)tempdist;	
		
		CirclePointslist currentpoints = new CirclePointslist();
		currentpoints.tapPosition = item.getPoint();
		currentpoints.radius = radius;
		selectedpoints.add(currentpoints);
		return true;
	} 
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		drawCircleDisplay(canvas, mapView, shadow);
	}

	private void drawCircleDisplay(Canvas canvas, MapView mapView, boolean shadow) {

	if(markerTapped){
		Projection projection = mapView.getProjection();
		if (shadow) {
			// Create and setup your paint brush
		    Paint paint = new Paint();
		   // paint.setARGB(255, 0, 255, 255);
		    paint.setColor(0x30000000);
		    paint.setStyle(Style.FILL_AND_STROKE);
		    for(int i=0; i<selectedpoints.size(); i++){
		    	Point tapPoint = projection.toPixels(selectedpoints.get(i).getTapPosition(), null);
				canvas.drawCircle((float)tapPoint.x, (float)tapPoint.y, selectedpoints.get(i).getRadius(), paint);
		    }
		/*    Point tapPoint = projection.toPixels(tapPosition, null);
			canvas.drawCircle((float)tapPoint.x, (float)tapPoint.y, radius, paint); */
		}		
	}
	super.draw(canvas, mapView, shadow);
	
}

	public void addOverlay(OverlayItem overlay) {
		mapOverlays.add(overlay);
		this.populate();
	}
}
